package it.camelotone.ejb.session;

import it.camelotone.ejb.entity.Friend;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class FriendEJB
 */
@Stateless
@LocalBean
@Named
public class FriendEJB {
	
	@PersistenceContext
	private EntityManager entityManager;
	
	@Inject
	private Friend friend_inj;

    /**
     * Default constructor. 
     */
    public FriendEJB() {
    }
    
    public void create() {
    	Friend friend = new Friend(friend_inj.getName(), friend_inj.getAge());
    	entityManager.persist(friend);
    }
    
    public List<Friend> getAll() {
    	@SuppressWarnings("unchecked")
		List<Friend> resultList = entityManager.createQuery(Salutation.QUERY_TXT).getResultList();
    	return resultList;
    }
    
    public String findById(int id) {
    	Friend friend = getAll().get(id);
    	return (friend != null) ? friend.getName() : "";
    }

}
